<template>
    <rx-dialog @handOk="okFun" @cancel="cancel">
        <rx-layout>
            <div slot="center" style>
                <rx-fit>
                    <div slot="toolheader" foldheader="true" foldbtn="true" border="false">
                        <div class="table-operator">
                            <a-button type="primary" icon="plus" @click="addUser">{{i18n('list.empowerUsers','授权用户')}}
                            </a-button>
                            <a-button type="primary" icon="plus" @click="addGroup">
                                {{i18n('list.empowerGroup','授权用户组')}}
                            </a-button>
                            <a-dropdown v-if="removeEnable && selectedRowKeys.length > 0">
                                <a-button type="danger" icon="delete" @click="delByIndexs(selectedRowKeys)">
                                    {{$ti18('rxList.delBtn','删除')}}
                                </a-button>
                            </a-dropdown>
                        </div>
                    </div>
                    <rx-grid
                        :allowRowSelect="true"
                        ref="table"
                        :multiSelect="true"
                        :columns="columns"
                        :defaultPageSize="10"
                        url="/api-bpm/bpm/core/bpmCalGrant/query"
                        :queryParam="queryParam"
                        data-field="result.data"
                        :pageSizeOptions="['10','30','40']"
                        rowKey="grantId"
                        @selectChange="onSelectChange"
                    >
          <span slot="grantType" slot-scope="{ text, record }" class="grantTypeIcons">
            <span v-if="record.grantType == 'USER'">{{i18n('user','用户')}}</span>
            <span v-if="record.grantType == 'GROUP'">{{i18n('group','用户组')}}</span>
          </span>
                        <span slot="action" slot-scope="{ text, record }" class="actionIcons">
            <span v-if="removeEnable" @click.stop="delByIds([record.grantId])">{{$ti18('rxList.delBtn','删除')}}</span>
          </span>
                    </rx-grid>
                </rx-fit>
            </div>
        </rx-layout>
    </rx-dialog>
</template>

<script>
    import BpmCalGrantApi from '@/api/bpm/core/bpmCalGrant'
    import {BaseList, Dialog, RxDialog, Util} from '@lowcode/jpaas-common-lib';

    export default {
        name: 'BpmCalGrantList',
        mixins: [BaseList],
        components: {
            RxDialog
        },
        props: {
            layerid: String,
            lydata: Object,
            destroy: Function,
            settingId: {
                type: String,
                default: ""
            }
        },
        created() {
            this.initQueryParam();
        },
        data() {
            return {
                columns: [
                    {
                        title: this.$ti18('rxList.index','序号'),
                        type: 'indexColumn',
                        width: 30,
                        dataIndex: 'serial',
                        scopedSlots: {customRender: 'serial'}
                    },
                    {title: this.i18n('form.grantType','分配类型'), dataIndex: 'grantType', width: 100, scopedSlots: {customRender: 'grantType'}},
                    {title: this.i18n('form.Assignee','被分配者'), dataIndex: 'belongWho', width: 100},
                    {title: this.$ti18('rxList.action','操作'), width: 100, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
                ],
                comment: this.i18n('list.calAllocate','日历分配'),
                widthHeight: ['800px', '600px']
            }
        },
        methods: {
            i18n(name, text, key) {
                return this.$ti18(name, text, "bpmCalSetting", key);
            },
            initQueryParam() {
                this.queryParam.Q_SETTING_ID__S_EQ = this.settingId;
            },
            addUser() {
                var conf = {curVm: this, data: {single: false}, widthHeight: ['1000px', '600px']};
                Dialog.openUserDialog(conf, function (self, users) {
                    if (!users || users.length == 0) {
                        return;
                    }
                    var addlist = [];
                    for (var i = 0; i < users.length; i++) {
                        var user = users[i];
                        if (!self.isContain(user.userId, "USER")) {
                            addlist.push(self.newCalGrantObj(user, "USER"));
                        }
                    }
                    if (addlist.length > 0) {
                        self.saveAddAll(addlist)
                    }
                });
            },
            newCalGrantObj(data, type) {
                return {
                    settingId: this.settingId,
                    belongWhoId: data.userId || data.groupId,
                    grantType: type,
                    belongWho: data.fullName || data.name,
                }
            },
            addGroup() {
                var conf = {
                    curVm: this,
                    data: {single: false, dimId: '1', showDimId: true},
                    widthHeight: ['900px', '600px']
                };
                Dialog.openGroupDialog(conf, function (self, groups) {
                    if (!groups || groups.length == 0) {
                        return false;
                    }
                    var addlist = [];
                    for (var i = 0; i < groups.length; i++) {
                        var group = groups[i];
                        if (!self.isContain(group.groupId, "GROUP")) {
                            addlist.push(self.newCalGrantObj(group, "GROUP"));
                        }
                    }
                    if (addlist.length > 0) {
                        self.saveAddAll(addlist)
                    }
                })
            },
            saveAddAll(addList) {
                var self_ = this;
                var parameter = {
                    addList: addList
                }
                BpmCalGrantApi.saveAddAll(parameter).then(res => {
                    self_.$refs.table.loadData();
                });
            },
            isContain(id, type) {
                var dataList = this.$refs.table.dataAction;
                if (!dataList || dataList.length == 0) {
                    return false;
                }
                var obj = dataList.find(item => (item.belongWhoId === id && item.grantType === type));
                if (!obj || !obj.grantId) {
                    return false;
                }
                return true;
            },
            delByIndexs(indexs) {
                let self_ = this;
                this.$confirm({
                    title: this.$ti18('rxMsa.actionTips','操作提示'),
                    content: this.$ti18('rxMsa.delInfo','您确定需要删除选中的记录吗？'),
                    okText: this.$ti18('rxForm.ackBtn','确认'),
                    cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                    zIndex:99999,
                    onOk() {
                        var dataList = self_.$refs.table.dataAction;
                        var delIds = [];
                        for (var i = 0; i < indexs.length; i++) {
                            var index = indexs[i];
                            var delObj = dataList.find(item => item.idx_ === index);
                            if (delObj && delObj.grantId) {
                                delIds.push(delObj.grantId);
                            }
                        }
                        self_.delById(delIds).then(res => {
                            if (res.success && res.code == 200) {
                                self_.refreshGrid()
                            }
                            self_.selectedRowKeys = []
                        })
                    },
                    onCancel() {
                    }
                })
            },
            delById(ids) {

                return BpmCalGrantApi.del({ids: ids.join(',')});
            },
            getListByParams(parameter) {
                return BpmCalGrantApi.query(parameter)
            },
            okFun() {
                Util.closeWindow(this, "ok");
            },
            cancel() {
                Util.closeWindow(this, "cancel");
            },
        }
    }
</script>
<style scoped>

    .grantTypeIcons {
        color: #2f54eb;
    }
</style>

